// lib/screens/auth/signup_screen.dart
import 'package:flutter/material.dart';

import '../../api/auth_api.dart';
import '../../models/response.dart';
import '../../models/user.dart';

class SignupScreen extends StatefulWidget {
  const SignupScreen({super.key});

  @override
  State<SignupScreen> createState() => _SignupScreenState();
}

class _SignupScreenState extends State<SignupScreen> {
  final TextEditingController _usernameController = TextEditingController();
  final TextEditingController _passwordController = TextEditingController();
  final TextEditingController _confirmPasswordController = TextEditingController();
  final TextEditingController _phoneController = TextEditingController(); // **新增：手机号控制器**

  final AuthApi _authApi = AuthApi();
  bool _isLoading = false;

  Future<void> _signup() async {
    if (_usernameController.text.isEmpty ||
        _passwordController.text.isEmpty ||
        _confirmPasswordController.text.isEmpty ||
        _phoneController.text.isEmpty // **新增：手机号非空校验**
    ) {
      _showSnackBar('所有字段都不能为空！');
      return;
    }

    if (_passwordController.text != _confirmPasswordController.text) {
      _showSnackBar('两次输入的密码不一致！');
      return;
    }

    setState(() {
      _isLoading = true;
    });

    try {
      final ResponseSuccess<User> response = await _authApi.signup(
        username: _usernameController.text,
        password: _passwordController.text,
        rePassword: _confirmPasswordController.text,
        phone: _phoneController.text, // **修改：将手机号传递给 API 方法**
      );
      if (response.code == 1000) {
        _showSnackBar('注册成功！请返回登录页面。');
        Navigator.of(context).pop(); // 注册成功后返回登录页面
      } else {
        _showSnackBar('注册失败: ${response.msg}');
      }
    } catch (e) {
      _showSnackBar('注册异常: $e');
    } finally {
      setState(() {
        _isLoading = false;
      });
    }
  }

  void _showSnackBar(String message) {
    ScaffoldMessenger.of(context).showSnackBar(
      SnackBar(content: Text(message)),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('注册')),
      body: _isLoading
          ? const Center(child: CircularProgressIndicator())
          : Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            TextField(
              controller: _usernameController,
              decoration: const InputDecoration(labelText: '用户名'),
            ),
            const SizedBox(height: 16),
            TextField(
              controller: _phoneController, // **新增：手机号输入框**
              decoration: const InputDecoration(labelText: '手机号'),
              keyboardType: TextInputType.phone, // 设置键盘类型为电话号码
            ),
            const SizedBox(height: 16),
            TextField(
              controller: _passwordController,
              decoration: const InputDecoration(labelText: '密码'),
              obscureText: true,
            ),
            const SizedBox(height: 16),
            TextField(
              controller: _confirmPasswordController,
              decoration: const InputDecoration(labelText: '确认密码'),
              obscureText: true,
            ),
            const SizedBox(height: 32),
            SizedBox(
              width: double.infinity,
              child: ElevatedButton(
                onPressed: _signup,
                child: const Text('注册'),
              ),
            ),
          ],
        ),
      ),
    );
  }
}